package com.ykdl.member.kid.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.bither.util.NativeUtil;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.chat.ChatConfig;
import com.ykdl.member.kid.prefs.MyPrefs;
/**
 * 拍照，选照片的工具类
 */
public class CameraHelp {
	public static final int SURVEY_CAMERA_WITH_DATA = 23; // 照相机
	public static final int SURVEY_PHOTO_WITH_DATA = 24; // 相册
	public static final int SURVEY_PHOTO_ZOOM = 25; // 缩放
	public static final int SURVEY_PHOTO_CROP = 26; 
	public final static String ACTION_CROP_IMAGE = "android.intent.action.kid.CROP";
	// 当前记录图片的url
	private static Uri mImageUrl, cametaImageUri;
	private Activity mCurrentAcitivy;
	// private View mOldView;// , mCameraView;
	private ImageView mOldImage;// , mCameraImage;
	// private Drawable oldDraw, cameraDraw;
	private Bitmap mBitmap, def_bg;
	private EditText editText;

	private RelativeLayout mInput;
	private Drawable inputBG;
	// private Drawable oldImBG;
	private String smallpicPath;
	private ImageView mask;

	private boolean isCurrentView;
	//private ICameraSave cameraSave;
	private boolean mNeedzoom;
	
	final private static int MAX_IMAGE_SIZE=100*1024;
	private String localCameraPath = "";// 拍照后得到的图片地址
	public void setmNeedzoom(boolean mNeedzoom) {
		this.mNeedzoom = mNeedzoom;
	}

	public RelativeLayout getmInput() {
		return mInput;
	}

	public void setmInput(RelativeLayout mInput) {
		this.mInput = mInput;
		inputBG = this.mInput.getBackground();
	}

	public CameraHelp(Activity context/* , ImageView imageview */) {
		mCurrentAcitivy = context;
	}

	public CameraHelp(Activity mCurrentAcitivy, ImageView oldImage) {
		super();
		this.mCurrentAcitivy = mCurrentAcitivy;
		mOldImage = oldImage;
		// init();
	}

	public CameraHelp(Activity mCurrentAcitivy, View mOldView) {
		super();
		this.mCurrentAcitivy = mCurrentAcitivy;
		// this.mOldView = mOldView;
		// init();
	}

	public void setDef_bg(Bitmap bitmap) {
		def_bg = bitmap;
	}

	public void setOldImage(ImageView img) {
		mOldImage = img;
	}

	// 跳转到拍照
	private String startCamera(String filepath, String name) {

		String path = "";
		try {

			String fileUri = filepath;
			File out = new File(fileUri);
			if (!out.exists()) {
				out.mkdirs();
			}
			String fileName = name;
			if (fileName == null || fileName.length() == 0) {
				fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";// 照片命名
			}

			out = new File(fileUri, fileName);
			fileUri = fileUri + fileName;// 该照片的绝对路径
			cametaImageUri = Uri.fromFile(out);
			MyPrefs.setCamePath(mCurrentAcitivy.getBaseContext(), cametaImageUri.toString());
			MyPrefs.setSmallCamePath(mCurrentAcitivy.getBaseContext(), smallpicPath);

			path = Uri.fromFile(out).toString();
			// set the // image // file

			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, cametaImageUri);
			intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
			mCurrentAcitivy.startActivityForResult(intent, SURVEY_CAMERA_WITH_DATA);
		} catch (Exception e) {
			Toast.makeText(mCurrentAcitivy, R.string.function_not_found, Toast.LENGTH_SHORT).show();
		}
		return path;
	}

	// 跳转到拍照
	public void startCamera(String path) {
		String[] dir = FileUtil.getImagePath(mCurrentAcitivy, path);
		smallpicPath = dir[0];
		startCamera(dir[0], dir[1]);
	}
	
	/**
	 * 启动相机拍照 startCamera
	 * 
	 * @Title: startCamera
	 * @throws
	 */
	public void selectImageFromCamera() {
		Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File dir = new File(ChatConfig.BMOB_PICTURE_PATH);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File file = new File(dir, String.valueOf(System.currentTimeMillis())
				+ ".jpg");
		localCameraPath = file.getPath();
		Uri imageUri = Uri.fromFile(file);
		openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		mCurrentAcitivy.startActivityForResult(openCameraIntent,
				ChatConfig.REQUESTCODE_TAKE_CAMERA);
	}
	public void startCamera(String path, boolean pneedzoom) {
		mNeedzoom = pneedzoom;
		String[] dir = FileUtil.getImagePath(mCurrentAcitivy, path);
		smallpicPath = dir[0];
		startCamera(dir[0], dir[1]);
	}
	/**
	 * 选择图片
	 * @Title: selectImage
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 */
	public void selectImageFromLocal() {
		Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
       		 "image/*");
		mCurrentAcitivy.startActivityForResult(intent, ChatConfig.REQUESTCODE_TAKE_LOCAL);
	}
	// 跳转到图片选择
	public void startPhoto(String path, boolean pneedzoom) {
		try {
			mNeedzoom = pneedzoom;
			startPhoto(path);

		} catch (ActivityNotFoundException e) {
			Toast.makeText(mCurrentAcitivy, R.string.function_not_found, Toast.LENGTH_SHORT).show();
		}
	}

	// 跳转到图片选择
	public void startPhoto(String path) {
		try {
			smallpicPath = Environment.getExternalStorageDirectory().toString() + path;
			Intent intent = new Intent(Intent.ACTION_PICK, null);
			// Intent intent = new Intent(mCurrentAcitivy,
			// Gallery3DActivity.class);
			intent.setType("image/*");
			mCurrentAcitivy.startActivityForResult(intent, SURVEY_PHOTO_WITH_DATA);
		} catch (ActivityNotFoundException e) {
			Toast.makeText(mCurrentAcitivy, R.string.function_not_found, Toast.LENGTH_SHORT).show();
		}
	}

	public CameraHelp(Activity mCurrentAcitivy, ImageView oldImage, ImageView mask) {
		super();
		this.mCurrentAcitivy = mCurrentAcitivy;
		mOldImage = oldImage;
		// oldImBG = mOldImage.getBackground();
		this.mask = mask;
	}

	// 得到图像选择
	public String setPicture(int requestCode, int resultCode, Intent data) {
		if (requestCode == ChatConfig.REQUESTCODE_TAKE_LOCAL) {
			Uri selectedImage = data.getData();
			String localSelectPath="";
			if (selectedImage != null) {
				Cursor cursor = mCurrentAcitivy.getContentResolver().query(
						selectedImage, null, null, null, null);
				if (null!=cursor) {
					cursor.moveToFirst();
					int columnIndex = cursor.getColumnIndex("_data");
					localCameraPath = cursor.getString(columnIndex);
					cursor.close();
//				nativeCompressBitmap(localCameraPath);
					return getPath(localCameraPath);
				}else {
//					/storage/emulated/0/DCIM/Camera/IMG_20150701_154229.jpg
					localCameraPath=selectedImage.toString().substring(7);
					getPath(localCameraPath);
//					file:///storage/emulated/0/MIUI/Gallery/cloud/.thumbnailFile/f339046ba9c96b1f5df52dfe96911dafb56fa873.jpg
				}
			}
		}
		
		return getPath(localCameraPath);
	}
	private Bitmap getImage(String srcPath) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		
		newOpts.inDither=false; 
		newOpts.inPurgeable=true; 
		newOpts.inTempStorage=new byte[12 * 1024];  
		//开始读入图片，此时把options.inJustDecodeBounds 设回true了
		newOpts.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空
		
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		//现在主流手机比较多是800*480分辨率，所以高和宽我们设置为
		float hh = 800f;//这里设置高度为800f
		float ww = 480f;//这里设置宽度为480f
		//缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
		int be = 1;//be=1表示不缩放
		if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
			be = (int) (newOpts.outWidth / ww);
		} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = 6;//设置缩放比例
		//重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
		File file = new File(srcPath); 
		FileInputStream fs=null; 
		Bitmap bmp = null; 
		try { 
		    fs = new FileInputStream(file); 
		    if(fs != null) 
		    	bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, newOpts); 
		     } catch (Exception e) { 
		    e.printStackTrace(); 
		}
//		if(!bitmap.isRecycled() ){
//			bitmap.recycle(); //回收图片所占的内存         
//			   android.provider.Settings.System.gc();  //提醒系统及时回收
//			}
//		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
	}
	private Bitmap comp(Bitmap image) {
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();		
		image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		if( baos.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片（BitmapFactory.decodeStream）时溢出	
			baos.reset();//重置baos即清空baos
			image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%，把压缩后的数据存放到baos中
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		//开始读入图片，此时把options.inJustDecodeBounds 设回true了
		newOpts.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		//现在主流手机比较多是800*480分辨率，所以高和宽我们设置为
		float hh = 800f;//这里设置高度为800f
		float ww = 480f;//这里设置宽度为480f
		//缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
		int be = 1;//be=1表示不缩放
		if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
			be = (int) (newOpts.outWidth / ww);
		} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;//设置缩放比例
		//重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
		isBm = new ByteArrayInputStream(baos.toByteArray());
		bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
		return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
	}
	private Bitmap compressImage(Bitmap image) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
		int options = 100;
		while ( baos.toByteArray().length / 1024>100) {	//循环判断如果压缩后图片是否大于100kb,大于继续压缩		
			baos.reset();//重置baos即清空baos
			image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
			options -= 10;//每次都减少10
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
		return bitmap;
	}
	public String getPath(String localCameraPath) {
		
		String dirFile = Environment.getExternalStorageDirectory().toString()+"/hsh/bmpcache/";
		File dirfile=new File(dirFile);
		if(!dirfile.exists()){
			dirfile.mkdirs();
		}
		File originalFile = new File(dirFile,System.currentTimeMillis()+".jpg");
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(
					originalFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Bitmap bit =getImage(localCameraPath);
		int angle= getExifOrientation(localCameraPath);
		if(angle!=0){  //如果照片出现了 旋转 那么 就更改旋转度数
			Matrix matrix = new Matrix();
			matrix.postRotate(angle);
			bit = Bitmap.createBitmap(bit,
					0, 0, bit.getWidth(), bit.getHeight(), matrix, true);
		}
		
//		bit.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
//		int options = 100;
		bit.compress(CompressFormat.JPEG, 80, fileOutputStream);
		return originalFile.getAbsolutePath();
	}
	/**
     * 得到 图片旋转 的角度
     * @param filepath
     * @return
     */
    private int getExifOrientation(String filepath) {
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
            Log.e("test", "cannot read exif", ex);
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
            }
        }
        return degree;
    }
	
			
		/*if (requestCode == SURVEY_CAMERA_WITH_DATA || requestCode == SURVEY_PHOTO_WITH_DATA) {

			if (resultCode == Activity.RESULT_OK) {
				// setCameraContentView();
				if (requestCode == SURVEY_CAMERA_WITH_DATA) {
					mImageUrl = cametaImageUri;
				} else {

				}

				if (data != null) {
					mImageUrl = data.getData();
				} else {
					mImageUrl = cametaImageUri;
				}
				if (mImageUrl == null) {
					mImageUrl = Uri.parse(MyPrefs.getCamePath(mCurrentAcitivy.getBaseContext()));
				}
  
				
				//从相册选择会用到这里
				String realpath="";				
				if(mImageUrl.toString().startsWith("content")){
					
		           	ContentResolver cr = mCurrentAcitivy.getContentResolver();
		        	Cursor cursor = cr.query(mImageUrl, null, null, null, null);
		        	cursor.moveToFirst();
		        	int index=	cursor.getColumnIndex("_data");
		           if(index!=-1){		            
		             realpath=cursor.getString(index);
			    	}
		          
		        	cursor.close();		        	
		        	
		        	if(realpath.length()>0){	
		        		Log.e("alan", "------------------realpath:"+realpath);
						String src=realpath;
						String dir=Environment.getExternalStorageDirectory()+KidConfig.BITMAP_CACHE_PATH;
						String des=dir+System.currentTimeMillis()+".jpg";
						File dirfile=new File(dir);
						if(!dirfile.exists()){
							dirfile.mkdirs();
						}
						FileUtil.copyFile(src,des);
//						mImageUrl=Uri.parse("file://"+des);
						mImageUrl=Uri.parse(realpath);
					}					
				}
				
				
				if (mNeedzoom) {
					QLog.debug("setPicture===============", mImageUrl.toString());
//					startPhotoZoom(mImageUrl);
				} else {
					picpath = saveSmallPic();
				}

			}

		} else if (requestCode == SURVEY_PHOTO_ZOOM) {
			// File oldImage = new File(mImageUrl.getPath());
			// if (oldImage.exists()) { //不要删除 大图片
			// oldImage.delete();
			// }
			if (data != null) {
				Bundle extras = data.getExtras();
				if (extras != null) {
					ByteArrayOutputStream stream = null;
					FileOutputStream fileOutputStream = null;
					try {
//						Bitmap photo = extras.getParcelable("data");
//						stream = new ByteArrayOutputStream();
//						photo.compress(Bitmap.CompressFormat.JPEG, 80, stream);
						if (mImageUrl == null) {

							mImageUrl = Uri.parse(MyPrefs.getCamePath(mCurrentAcitivy.getBaseContext()));
						}
						if (smallpicPath == null) {
							smallpicPath = MyPrefs.getSmallCamePath(mCurrentAcitivy.getBaseContext());
						}
						
						String imgpath=mImageUrl.toString().toString();
//						String imgpath=mImageUrl.toString().toString().substring("file://".length());
						File file=new File(imgpath);
						FileInputStream in=new FileInputStream(file);
						if(in.available()>100*1024){
							Bitmap bit=	BitmapFactory.decodeFile(imgpath);
							int yasuo=100*MAX_IMAGE_SIZE/in.available();
							
							String path = new String(imgpath);
						
							String fileName = path.replace(".jpg", "s.jpg");
//							File f = new File(fileName);
//							if (!f.exists()) {
//								f.mkdirs();
//							}
							picpath =  fileName;
							File out=new File(fileName);
							if(!out.exists()){          
								out.createNewFile();
							}
							fileOutputStream=new FileOutputStream(out);
							
							bit.compress(CompressFormat.JPEG, yasuo, fileOutputStream);
							bit.recycle();
							mImageUrl=Uri.parse(picpath);
						}
						
					
						
//						mImageUrl = Uri.fromFile(temp);
//						fileOutputStream = new FileOutputStream(temp);
//						fileOutputStream.write(stream.toByteArray());
//						fileOutputStream.flush();
					//	mBitmap = photo;

					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						try {
							fileOutputStream.close();
							stream.close();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
			}
		}else if (requestCode == SURVEY_PHOTO_CROP) {
			// File oldImage = new File(mImageUrl.getPath());
			// if (oldImage.exists()) { //不要删除 大图片
			// oldImage.delete();
			// }
			if (data != null) {
				picpath=data.getStringExtra("path");
			}
		}
//		addImage();
*/		
		//return mImageUrl.getPath();

	private int outputX = 300;
	private int outputY = 300;

	public void setOutputX(int outputX) {
		this.outputX = outputX;
	}

	public void setOutputY(int outputY) {
		this.outputY = outputY;
	}

	private int aspectX = 5;
	private int aspectY = 5;

	public void setAspectX(int aspectX) {
		this.aspectX = aspectX;
	}

	public void setAspectY(int aspectY) {
		this.aspectY = aspectY;
	}

	public void startPhotoZoom(Uri uri) {
		if (uri != null) {
			uri = Uri.parse("file://"+uri.toString());
			Intent intent = new Intent("com.android.camera.action.CROP");
			intent.setDataAndType(uri, "image/*");
			intent.putExtra("crop", "false");
			// aspectX aspectY 是宽高的比例
			intent.putExtra("aspectX", aspectX);
			intent.putExtra("aspectY", aspectY);
			// outputX outputY 是裁剪图片宽高
			intent.putExtra("outputX", outputX);
			intent.putExtra("outputY", outputY);
			intent.putExtra("mypicth", mImageUrl);
			intent.putExtra("scale", true);
			intent.putExtra("return-data", false);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUrl);
			intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
			intent.putExtra("noFaceDetection", true); // no face detection
			mCurrentAcitivy.startActivityForResult(intent, SURVEY_PHOTO_ZOOM);
		}
	}
	public void startPhotoZoom1(Uri uri) {
		if (uri != null) {
			Intent intent = new Intent(ACTION_CROP_IMAGE);;
			intent.putExtra("789", uri);
			intent.putExtra("smallpicPath", smallpicPath);
//			intent.setDataAndType(uri, "image/*");
//			intent.putExtra("crop", "true");
//			// aspectX aspectY 是宽高的比例
//			intent.putExtra("aspectX", aspectX);
//			intent.putExtra("aspectY", aspectY);
//			// outputX outputY 是裁剪图片宽高
//			intent.putExtra("outputX", outputX);
//			intent.putExtra("outputY", outputY);
//			intent.putExtra("mypicth", mImageUrl);
//			intent.putExtra("scale", true);
//			intent.putExtra("return-data", true);
			mCurrentAcitivy.startActivityForResult(intent, SURVEY_PHOTO_CROP);
		}
	}

	final static int photo_wid = 480;
	final static int photo_hei = 800;

	private String saveSmallPic() {
		String picpath = "";
		String imageUrl = mImageUrl.getPath();

		ContentResolver resolver = mCurrentAcitivy.getContentResolver();
		try {
			if (mImageUrl != null) {
				// 清楚上一个bitmap
				/*
				 * if (mBitmap != null) { mBitmap.recycle(); }
				 */
			}
			// 这里是拍照
			if (imageUrl.contains(".jpg")) {
				mBitmap = BitmapManager.decreaseBitmap(imageUrl);
				BitmapManager.saveBitmap(imageUrl.replace(".jpg", "s.jpg"), mBitmap);
				picpath = imageUrl.substring(0, imageUrl.indexOf(".jpg")) + "s.jpg";
				mImageUrl = Uri.parse(picpath);
			} else {
				// 这里是选照片
				checkMapTooBig(resolver);

				mBitmap = MThumbnailPhoto.lessBitmap(mBitmap, photo_wid, photo_hei);
				String fileUri = smallpicPath;
				File f = new File(fileUri);
				if (!f.exists()) {
					f.mkdirs();
				}
				String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
				if(!fileUri.endsWith("/")){
					fileUri=fileUri+"/";
				}
				MThumbnailPhoto.saveMyBitmap(fileUri + fileName, mBitmap);
				picpath = fileUri + fileName + "s.jpg";
				mImageUrl = Uri.parse(picpath);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return picpath;
	}

	private void checkMapTooBig(ContentResolver resolver) {
		try {
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(resolver.openInputStream(mImageUrl), null, opts);
			if (opts.outHeight * opts.outWidth > photo_wid * photo_hei) {// 480*800像素
				opts.inJustDecodeBounds = false;
				opts.inSampleSize = 6;
				mBitmap = BitmapFactory.decodeStream(resolver.openInputStream(mImageUrl), null, opts);
			} else {
				mBitmap = BitmapFactory.decodeStream(resolver.openInputStream(mImageUrl));
			}

		} catch (Exception e) {

		}
		System.gc();
	}

	public void addImage() {
		if (mBitmap != null) {
			if (mOldImage != null) {
				mOldImage.setImageBitmap(mBitmap);
				// mCameraImage.setBackgroundDrawable(null);
				// mCameraImage.setImageBitmap(mBitmap);
				if (mask != null) {
					mask.setVisibility(View.VISIBLE);
				}
			}
			if (mInput != null) {
				mInput.setBackgroundDrawable(new BitmapDrawable(mBitmap));
			}

//			if (cameraSave != null) {
//				cameraSave.onCameraCome(mImageUrl.toString());
//			}

			mBitmap = null;

		} else {
			imageReset();
		}
	}

	// 重置imageView
	public void imageReset() {
		/*
		 * if (mBitmap != null) { mBitmap.recycle(); }
		 */
		if (mOldImage != null) {
			mOldImage.setImageBitmap(def_bg);
			// mCameraImage.setImageBitmap(null);
			// mCameraImage.setBackgroundDrawable(cameraDraw);
			if (mask != null) {
				mask.setVisibility(View.GONE);
			}
		}
		if (mInput != null) {
			mInput.setBackgroundDrawable(inputBG);
		}
	}

	// 设置图像
	public void setImageUrl(String url) {
		if (url == null) {
			mImageUrl = null;
			MyPrefs.setCamePath(mCurrentAcitivy.getBaseContext(), "");
			MyPrefs.setSmallCamePath(mCurrentAcitivy.getBaseContext(), "");
		} else {
			MyPrefs.setCamePath(mCurrentAcitivy.getBaseContext(), Uri.parse(url).toString());
			mImageUrl = Uri.parse(url);
		}
	}

	// 图像url
	public String getImageUrl() {
		// TODO Auto-generated method stub
		if (mImageUrl == null) {
			return "null";
		}
		return mImageUrl.toString();
	}

	// 得到图像简介
	public String getEditText() {
		// String intro = editText.getText().toString();
		// if (intro == null || intro.length() == 0) {
		// intro = "null";
		// }
		return "辣妈育儿";
	}

	// 设置图像简介
	public void setEditText(String text) {
		if (text == null || "null".equals(text) || text.contains("忘记照片简介了")) {
			text = "";
		}
		if (editText != null) {
			editText.setText(text);
		}
	}

	public boolean getCurrentView() {
		// TODO Auto-generated method stub
		return isCurrentView;
	}

//	public void setCameraSave(ICameraSave iCameraSave) {
//		// TODO Auto-generated method stub
//		cameraSave = iCameraSave;
//	}
}
